<?php

class InMongo {

    private $db;

    function __construct() {
        $op = parse_ini_file(dirname(__FILE__) . "/conf.ini", true);

        $ip = $op['mongodb']['ip'];
        $name = $op['mongodb']['name'];
        $pwd = $op['mongodb']['pwd'];
        $dbname = $op['mongodb']['dbname'];
        $port = $op['mongodb']['port'];

        if ($name) {
            $s = "mongodb://$name:$pwd@$ip";
        } else {
            $s = "mongodb://$ip:$port";
        }
        try {
            $m = new MongoClient($s);
            $this->db = $m->selectDB($dbname);
        } catch (Exception $e) {
            $res = array();
            $res['code'] = 1;
            $res['msg'] = "mongodb not start " . $e->getMessage();
            echo json_encode($res);
        }
    }

    public function getMongoDb() {
        return $this->db;
    }

    public function object_array($array) {
        if (is_object($array)) {
            $array = (array) $array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
}

?>